package siddur.traktask.facade;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import siddur.traktask.entity.TaskInfo;
import siddur.traktask.repository.TaskRepository;
import siddur.traktask.util.TaskUtil;

@Service
public class TaskFacade {

	@Autowired
	TaskRepository taskRep;
	
	public TaskInfo getTask(String token){
		if(StringUtils.isNotBlank(token)){
			int i = token.indexOf("_");
			if(i != -1){
				String idString = token.substring(i + 1);
				Long taskId = TaskUtil.toLong(idString);
				if(taskId != -1){
					TaskInfo task = taskRep.findOne(taskId);
					if(task != null && token.equals(task.getToken())){
						return task;
					}
				}
			}
		}
		return null;
	}
}
